﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class BoPhanDetailView : Form
    {
        public event EventHandler<SaveChangeEventArgs> SaveChanged;
        public event EventHandler DeleteChanged;

        private BindingList<BoPhan> bindingBoPhan;
        private BindingList<BoPhan> bindingListBoPhan;
        private BoPhan _boPhan;

        public BoPhanDetailView()
        {
            InitializeComponent();
        }
        public BoPhanDetailView(BoPhan boPhan)
        {
            InitializeComponent();

            
            bindingBoPhan = new BindingList<BoPhan>();
            bindingListBoPhan = new BindingList<BoPhan>(DataAccessObject.BoPhan.GetListBoPhan());
            
            _boPhan = boPhan;
            if (_boPhan.TrucThuoc != null)
                gridTrucThuoc.EditValue = _boPhan.TrucThuoc.Oid;
            cboLoaiBoPhan.SelectedIndex = (int)_boPhan.LoaiBoPhan;

            bindingBoPhan.Add(_boPhan);

            bindingSourceBoPhan.DataSource = bindingBoPhan;
            bindingSourceTrucThuoc.DataSource = bindingListBoPhan;
            layoutControl1.Refresh();
            
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteChange();
            this.Close();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void btnLuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_boPhan != null)
                Luu();
            this.Close();
        }

        private void Luu()
        {
            if (_boPhan.Oid == -1)
            {
                DataAccessObject.BoPhan.Insert(_boPhan);
                SaveChange(true);
            }
            else
            {
                DataAccessObject.BoPhan.Update(_boPhan);
                SaveChange(false);
            }
        }
        private void SaveChange(bool isNew)
        {
            if (SaveChanged != null)
                SaveChanged(this, new SaveChangeEventArgs(isNew));
        }
        private void DeleteChange()
        {
            if (DeleteChanged != null)
                DeleteChanged(this, EventArgs.Empty);
        }

        private void BoPhanDetailView_Load(object sender, EventArgs e)
        {
            AppGridView.InitGridView(gridViewTrucThuoc, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            AppGridView.ShowField(gridViewTrucThuoc, new string[] { "MaQuanLy", "TenBoPhan" }, new string[] { "Mã quản lý", "Tên bộ phận" });

            cboLoaiBoPhan.SelectedIndex = 0;
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Luu();
        }

        private void txtLoaiBoPhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void gridTrucThuoc_EditValueChanged(object sender, EventArgs e)
        {
            BoPhan bp = gridViewTrucThuoc.GetFocusedRow() as BoPhan;
            if (bp != null)
                _boPhan.TrucThuoc = bp;
        }

        private void cboLoaiBoPhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoaiBoPhan.SelectedIndex >= 0)
                _boPhan.LoaiBoPhan = (ELoaiBoPhan)cboLoaiBoPhan.SelectedIndex;
        }

        private void gridTrucThuoc_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind== DevExpress.XtraEditors.Controls.ButtonPredefines.Undo)
            {
                _boPhan.TrucThuoc = null;
                gridTrucThuoc.EditValue = null;
            }
        }
    }
}
